package com.financing.wap.web.controller.adminapi;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.DstArticleCategoriesDTO;
import com.financing.wap.service.ICommonService;
import com.financing.wap.service.IDstArticleCategoriesService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.util.PinyinUtil;
import com.financing.wap.web.controller.BaseController;

@Controller("adminDstArticleCategoriesController")
@RequestMapping("/adminapi/dstArticleCategories")
public class DstArticleCategoriesController extends BaseController  {

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IDstArticleCategoriesService dstArticleCategoriesService;
	
	@Autowired
	private ICommonService commonService;
	
	@RequestMapping("/listDstArticleCategories.shtml")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, DstArticleCategoriesDTO dstArticleCategories,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query dstArticleCategories list begin .......");
			
			dstArticleCategories.setBegin(pager.getStartIndex());
			dstArticleCategories.setPageSize(pager.getPageSize());
			
			int count = commonService.allArticleTypeInfoCount(dstArticleCategories);
			
			pager.setTotalCount(count);
			
			List<DstArticleCategoriesDTO> dstArticleCategoriesList = null;
			
			if(count > 0){
				dstArticleCategoriesList = commonService.allArticleTypeInfo(dstArticleCategories);
			}else{
				dstArticleCategoriesList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", dstArticleCategoriesList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query dstArticleCategories list end .......");
		}catch(Exception e){
			log.error("query dstArticleCategories error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/addDstArticleCategories.shtml")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, DstArticleCategoriesDTO dstArticleCategories){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add dstArticleCategories  begin .......");
			
			//判断是否重复
			if(StringUtils.isBlank(dstArticleCategories.getArticleName())){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			dstArticleCategories.setArticleType(PinyinUtil.convert(dstArticleCategories.getArticleName()));
			DTOdateSetUtil.setCreateInfo(dstArticleCategories);
			DTOdateSetUtil.setUpdateInfo(dstArticleCategories);
			
			try {
				int id = dstArticleCategoriesService.insert(dstArticleCategories);
				result.setAttribute("id", id);
			} catch (DuplicateKeyException e) {
				result.setStatusCode(ResultMap.STATUS_CODE_DUPLICATEKEY);
				result.setStatus(ResultMap.STATUS.SUCCESS);
				return result;
			}
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add dstArticleCategories list end .......");
		}catch(Exception e){
			log.error("add dstArticleCategories error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateDstArticleCategories.shtml")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, DstArticleCategoriesDTO dstArticleCategories){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update dstArticleCategories  begin .......");
			
			Integer id = dstArticleCategories.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			//判断是否修改名称
//			if(StringUtils.isNotBlank(dstArticleCategories.getArticleName())){
//				dstArticleCategories.setArticleType(PinyinUtil.convert(dstArticleCategories.getArticleName()));
//			}

			
			DTOdateSetUtil.setUpdateInfo(dstArticleCategories);
			try {
				dstArticleCategoriesService.update(dstArticleCategories);
			} catch (DuplicateKeyException e) {
				result.setStatusCode(ResultMap.STATUS_CODE_DUPLICATEKEY);
				result.setStatus(ResultMap.STATUS.SUCCESS);
				return result;
			}
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update dstArticleCategories list end .......");
		}catch(Exception e){
			log.error("update dstArticleCategories error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
